Ext.define('jwawa.erp.cfg.searchPage.SearchPageListHandler', {
    extend: 'jwawa.erp.framework.ShellHandler',
    entities: ['cfg_search_pages', 'cfg_search_items'],
    resources: [
        {
            name: 'searchPageModel',
            type: 'object',
            res: 'jwawa.erp.cfg.searchPage.SearchPageModel'
        },
        {
            name: 'searchPageStore',
            type: 'store',
            res: 'Ext.data.Store',
            cfg: {
                fields: [
                    {name: 'id',  type: 'int'},
                    {name: 'page_name',  type: 'string'},
                    'page_code'
                ],
                proxy:{
                    type: 'jwawaProxy',
                    url : '/search-page/find-search-page-list',
                    reader: {
                        type: 'json',
                        root: 'data'
                    }
                },
                pageSize: DEFAULT_PAGE_SIZE,
                autoLoad: false
            }
        }
    ],
    ref:{
        searchPageQueryPanel: '#searchPageQueryPanel',
        searchPageDetailEditPanel: '#searchPageDetailEditPanel',

        searchPanel: "#searchPageQueryPanel searchPanel",
        searchPageGridPanel: '#searchPageQueryPanel #searchPageGridPanel',

        pagingtoolbar: '#searchPageGridPanel #pagingtoolbar',
        viewSearchPageButton: '#searchPageGridPanel #viewSearchPageButton',
        newSearchPageButton: '#searchPageGridPanel #newSearchPageButton',
        delSearchPageButton: '#searchPageGridPanel #delSearchPageButton'
    },

    constructor: function(config){
        var me = this;

        Ext.apply(me, config);
        me.callParent(arguments);
    },

    bindHandler: function(){
        "use strict";
        var me = this;

        me.initSearchPageGridPanel();
        me.view.setPageState('init');
    },

    onActiveSearchPageQueryPanel: function(){
        "use strict";
        var me = this,
            sm = me.searchPageGridPanel.getSelectionModel();

        // fix select missing after ajax requested
        sm.select(sm.getSelection());
    },

    initSearchPageGridPanel: function(){
        "use strict";
        var me = this,
            searchPageModel = Ext.JwawaResMgr.get('searchPageModel'),
            searchPageStore = Ext.JwawaResMgr.get('searchPageStore');

//      var searchPageGridStore = me.searchPageGridPanel.getStore();
//      searchPageGridStore.pageSize=3;

        me.searchPageGridPanel.reconfigure(searchPageStore);
        me.pagingtoolbar.bind(searchPageStore);

        searchPageStore.addListener('beforeload', me.onBeforeSearchPageStoreLoad, me);
        searchPageStore.addListener('load', me.onSearchPageStoreLoad, me);

        var searchPageSm = me.searchPageGridPanel.getSelectionModel();
        searchPageSm.on('selectionchange', me.onSelectChangeSearchPage, me);

        me.searchPanel.on('search', me.onSearch, me);
        me.viewSearchPageButton.on('click', me.onClickViewSearchPageButton, this);
        me.newSearchPageButton.on('click', me.onClickNewSearchPageButton, this);
        me.delSearchPageButton.on('click', me.onClickDelSearchPageButton, this);
        me.searchPageGridPanel.on('itemdblclick', me.onItemDbClickSearchPage, this);
        me.searchPageQueryPanel.addListener('activate', me.onActiveSearchPageQueryPanel, me);
    },

    onSearchPageStoreLoad: function(){
        var me = this;

        me.view.setLoading(false);
        return true;
    },

    onBeforeSearchPageStoreLoad: function(){
        var me = this;

        me.view.setLoading(true);
    },

    onSearch: function(spanel, searchData){
        "use strict";
        var me = this,
            searchPageStore = Ext.JwawaResMgr.get('searchPageStore'),
            page = 1;

//            searchPageStore.load({params:{ss: 'xx'}, page:2, start: 1, limit: 2});
        searchPageStore.getProxy().extraParams = searchData;
        searchPageStore.loadPage(page);
    },

    onSelectChangeSearchPage: function(obj, selected, eOpts){
        "use strict";
        var me = this;

        me.view.setPageState('deselect');
        if(selected.length > 0){
            me.view.setPageState('select');
        }
    },

    onItemDbClickSearchPage: function( obj, record, item, index, e, eOpts ){
        "use strict";
        var me = this;

        var pageId = record.get('id');
        me.viewSearchPage(pageId);
    },

    onClickViewSearchPageButton: function(){
        "use strict";
        var me = this,
            sm = me.searchPageGridPanel.getSelectionModel(),
            record = sm.getSelection()[0];

        var pageId = record.get('id');
        me.viewSearchPage(pageId);
    },

    viewSearchPage: function(pageId){
        "use strict";
        var me = this;

        me.searchPageDetailEditPanel.handler.editPageContent(pageId);
    },

    onClickNewSearchPageButton: function(){
        "use strict";
        var me = this;

        me.searchPageDetailEditPanel.handler.newPageContent();
    },

    onClickDelSearchPageButton: function(){
        "use strict";
        var me = this,
            sm = me.searchPageGridPanel.getSelectionModel(),
            searchPageModel = Ext.JwawaResMgr.get('searchPageModel'),
            searchPageStore = Ext.JwawaResMgr.get('searchPageStore');

        var searchPages = sm.getSelection();
        var searchPageIds = [];
        Ext.each(searchPages, function(searchPage){
            searchPageIds.push(searchPage.get('id'));
        });

        alertConfirm('提示','真的要执行删除操作吗？', function(buttonId){
            if(buttonId == 'yes'){
                me.view.setLoading(true);
                searchPageModel.deleteSearchPage(searchPageIds,{
                    success : function(pageIds) {
                        Ext.each(pageIds, function(pageId){
                            var searchPage = searchPageStore.getById(pageId);
                            searchPageStore.remove(searchPage);
                        });

                        me.searchPageDetailEditPanel.handler.clearPageContent();
                        me.searchPageDetailEditPanel.setPageState('init');
                        top.msgBar.showSuccessMsg('成功删除指定检索页记录');
                    },
                    callback: function(){
                        me.view.setLoading(false);
                    }
                });
            }
        });
    },

    onPageStateChange: function(obj, newState, oldState){
        "use strict";
        var me = this;

        switch(newState){
            case 'init':
                me.viewSearchPageButton.disable();
                me.delSearchPageButton.disable();
                break;
            case 'select':
                me.viewSearchPageButton.enable();
                me.delSearchPageButton.enable();
                break;
            case 'deselect':
                me.viewSearchPageButton.disable();
                me.delSearchPageButton.disable();
                break;
        }
    }
});